//
//  Task.swift
//  MySwiftUIDemo
//
//  Created by dn56-macbook on 2025/3/16.
//
import SwiftUI
struct Item{
    var width: Int = 0
    var height: Int = 0
    var name: String = ""
//    var uuid: UUID = UUID()
}
struct ItemInfo{
    var listData: [Item] = []
    var count: Int = 0
}
struct ItemInfo23{
    var listData: [[Item]] = []
}
struct Task{
    func getR(_ port: Bool)->CGFloat{
        return (port ? 228 : 264) / 364
    }
    func create2DArray(with elements: [Item], targetSum: Int) -> [[Item]] {
        var result: [[Item]] = []
        var currentSubArray: [Item] = []
        var remainingElements = elements
        
        while remainingElements.count > 0 {
            var canAddElement = false
            for i in 0..<remainingElements.count {
                let element = remainingElements[i]
                if currentSubArray.isEmpty || (getSum(currentSubArray) + element.width * element.height <= targetSum) {
                    currentSubArray.append(element)
                    remainingElements.remove(at: i)
                    canAddElement = true
                    break
                }
            }
            if !canAddElement && !currentSubArray.isEmpty {
                result.append(currentSubArray)
                currentSubArray = []
                continue
            }
            if currentSubArray.count > 0 && remainingElements.isEmpty {
                result.append(currentSubArray)
                currentSubArray = []
            }
        }
        if currentSubArray.count > 0 {
            result.append(currentSubArray)
        }
        return result
    }
    func getSum(_ list: [Item])->Int{
        var sum = 0
        list.forEach { item in
            sum = sum + item.width*item.height
        }
        return sum
    }
    func getTotalSum(_ list: [[Item]]) ->Int{
        let listData = list.flatMap({$0})
        return getSum(listData)
    }
    func haveData(_ list: [Item], sum: Int, count: Int)->Bool{
        let li = list.filter { objc in
            return objc.width * objc.height == sum
        }
        return li.count == count
    }
    func haveData(_ list: [Item], sum: Int)->Int{
        let li = list.filter { objc in
            return objc.width * objc.height == sum
        }
        return li.count
    }
    func getDatas(_ list: [Item],sum: Int)->[Item]{
        let li = list.filter { objc in
            return objc.width * objc.height == sum
        }
        return li
    }
}
